import type {
  IBadgeConfig,
  INavigatorBarItemConfig,
  IScriptNavigationItem,
} from '../../../../../../components/navigator_bar/types'
import type { QuickSearchTag } from '../../../../../../components/search/types'
import type { BadgeStyleConfig } from '../../../../../../types/interfaces'
import { ToolKit } from '../../../../../../lib/utils/toolkit'
import { BodyClasses } from '../../../../../../types/constants'

export const quickSearchTags: QuickSearchTag[] = [
  {
    type: 'high-quality',
    label: '高质量',
    bgColor: 'rgba(76, 175, 80, 0.1)',
    textColor: '#4caf50',
    hoverBgColor: 'rgba(76, 175, 80, 0.2)',
  },
  {
    type: 'popular',
    label: '热门',
    bgColor: 'rgba(255, 152, 0, 0.1)',
    textColor: '#ff9800',
    hoverBgColor: 'rgba(255, 152, 0, 0.2)',
  },
  {
    type: 'recommended',
    label: '推荐',
    bgColor: 'rgba(233, 30, 99, 0.1)',
    textColor: '#e91e63',
    hoverBgColor: 'rgba(233, 30, 99, 0.2)',
  },
  {
    type: 'high-score', // 新增高分标签
    label: '85分及以上',
    bgColor: 'rgba(255, 193, 7, 0.1)',
    textColor: '#ffc107',
    hoverBgColor: 'rgba(255, 193, 7, 0.2)',
    matchCondition: (item: IScriptNavigationItem) => item.score >= 85,
  },
  {
    type: 'frequently-updated',
    label: '近期更新',
    bgColor: 'rgba(156, 39, 176, 0.1)',
    textColor: '#9c27b0',
    hoverBgColor: 'rgba(156, 39, 176, 0.2)',
  },
  {
    type: 'new',
    label: '新上架',
    bgColor: 'rgba(33, 150, 243, 0.1)',
    textColor: '#2196f3',
    hoverBgColor: 'rgba(33, 150, 243, 0.2)',
  },
  {
    type: 'mine',
    label: '我的',
    bgColor: 'rgba(255, 193, 7, 0.15)',
    textColor: '#ff8f00',
    hoverBgColor: 'rgba(245, 124, 0, 0.25)',
  },
  {
    type: 'like',
    label: '喜欢',
    bgColor: 'rgba(244, 67, 54, 0.1)',
    textColor: '#f44336',
    hoverBgColor: 'rgba(244, 67, 54, 0.2)',
  },
  {
    type: 'dislike',
    label: '不喜欢',
    bgColor: 'rgba(211, 47, 47, 0.1)',
    textColor: '#d32f2f',
    hoverBgColor: 'rgba(211, 47, 47, 0.2)',
  },
  {
    type: 'success',
    label: '正常',
    bgColor: 'rgba(96, 125, 139, 0.1)',
    textColor: '#607d8b',
    hoverBgColor: 'rgba(96, 125, 139, 0.2)',
  },
  {
    type: 'failed',
    label: '已失效',
    bgColor: 'rgba(109, 76, 65, 0.1)',
    textColor: '#6d4c41',
    hoverBgColor: 'rgba(109, 76, 65, 0.2)',
  },
]

export const RENDER_CONFIG: INavigatorBarItemConfig<IScriptNavigationItem>[] = [
  {
    selector: '.meta',
    items: [
      {
        type: 'container',
        class: 'tags-container',
        children: [
          {
            selector: '.tags',
            items: [
              {
                type: 'stat',
                stats: quickSearchTags.map(tag => ({
                  key: `tag-${tag.type}` as keyof IScriptNavigationItem,
                  icon: '',
                  contentClass: 'content',
                  defaultValue: '',
                  shouldRender: (value: any) => !!value,
                  formatter: (value: string) => value || '',
                })),
              },
            ],
          },
        ],
      },
      {
        type: 'container',
        class: 'header',
        children: [
          {
            selector: '.header',
            items: [
              {
                type: 'link',
                linkSource: 'authorUrl',
                key: 'author',
                class: 'author',
                fallback: '未知作者',
                format: (value: string) => value || '未知作者',
              },
              {
                type: 'link',
                linkSource: 'url',
                key: 'id',
                class: 'script-id',
                fallback: 'NA',
                format: (value: string) => value || 'NA',
              },
            ],
          },
        ],
      },
      {
        type: 'description',
        key: 'name',
        class: 'name',
        fallback: '未知脚本',
        format: (value: string) => value || '未知脚本',
      },
      {
        type: 'description',
        key: 'description',
        class: 'description',
        fallback: '无描述',
        format: (value: string) => value || '无描述',
        syncToTitle: true,
      },
      {
        type: 'container',
        class: 'stats',
        children: [
          {
            selector: '.stats',
            items: [
              {
                type: 'stat',
                stats: [
                  {
                    key: 'score',
                    icon: '',
                    contentClass: 'count',
                    defaultValue: '0',
                    formatter: (value: string) => value || '0',
                  },
                  {
                    key: 'dailyInstalls',
                    icon: '',
                    contentClass: 'count',
                    defaultValue: '0',
                    formatter: (value: string) => value || '0',
                  },
                  {
                    key: 'totalInstalls',
                    icon: '',
                    contentClass: 'count',
                    defaultValue: '0',
                    formatter: (value: string) => value || '0',
                  },
                  {
                    key: 'goodRating',
                    icon: '',
                    contentClass: 'count',
                    defaultValue: '0',
                    shouldRender: (value: any) => value && value !== '0' && value !== 0,
                    formatter: (value: string) => value || '0',
                  },
                  {
                    key: 'badRating',
                    icon: '',
                    contentClass: 'count',
                    defaultValue: '0',
                    shouldRender: (value: any) => value && value !== '0' && value !== 0,
                    formatter: (value: string) => value || '0',
                  },
                  {
                    key: 'version',
                    icon: '',
                    contentClass: 'content',
                    defaultValue: '未知',
                    formatter: (value: string) => value || '未知',
                  },
                  {
                    key: 'updateTime',
                    icon: '',
                    contentClass: `content ${BodyClasses.OVERFLOW_VISIBLE}`,
                    defaultValue: '未知',
                    formatter: (value: string) => `${value}（${ToolKit.time.formatRelativeTime(value)}）` || '未知',
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
]

export const sortOptions: {
  value: string
  label: string
}[] = [
  { value: 'score', label: '按评分' },
  { value: 'daily-installs', label: '按日安装量' },
  { value: 'total-installs', label: '按总安装量' },
  { value: 'good-rating', label: '按好评' },
  { value: 'bad-rating', label: '按差评' },
  { value: 'updated', label: '按更新时间' },
]

export const badgeStyleConfigs: BadgeStyleConfig[] = [
  {
    type: 'popular',
    backgroundColor: '#fff8e1',
    textColor: '#ff8f00',
    borderColor: '#ffd54f',
    defaultDisplay: 'inline-block',
  },
  {
    type: 'high-quality',
    backgroundColor: '#e8f5e9',
    textColor: '#2e7d32',
    borderColor: '#81c784',
    defaultDisplay: 'inline-block',
  },
  {
    type: 'new',
    backgroundColor: '#e3f2fd',
    textColor: '#1565c0',
    borderColor: '#90caf9',
    defaultDisplay: 'inline-block',
  },
  {
    type: 'frequently-updated',
    backgroundColor: '#f3e5f5',
    textColor: '#7b1fa2',
    borderColor: '#ba68c8',
    defaultDisplay: 'inline-block',
  },
  {
    type: 'mine',
    backgroundColor: 'rgba(255, 193, 7, 0.15)',
    textColor: '#ff8f00',
    borderColor: '#ffd54f',
    defaultDisplay: 'inline-block',
  },
  {
    type: 'like',
    backgroundColor: '#ffebee',
    textColor: '#c62828',
    borderColor: '#ef9a9a',
    defaultDisplay: 'inline-block',
  },
  {
    type: 'dislike',
    backgroundColor: '#ffebee',
    textColor: '#d32f2f',
    borderColor: '#ef9a9a',
    defaultDisplay: 'inline-block',

  },
  {
    type: 'success',
    backgroundColor: '#eceff1',
    textColor: '#455a64',
    borderColor: '#b0bec5',
    defaultDisplay: 'inline-block',
  },
  {
    type: 'failed',
    backgroundColor: '#efebe9',
    textColor: '#6d4c41',
    borderColor: '#bcaaa4',
    defaultDisplay: 'inline-block',
  },
]

// 基础信息配置
export const baseFields: any = [
  { selector: '.avatar', attr: 'src', fallback: '' },
  { selector: '.name', prop: 'text', fallback: '未知', syncToTitle: true },
  { selector: '.description', prop: 'text', fallback: '无描述', syncToTitle: true },
  { selector: '.author', prop: 'text', attr: 'href', fallback: '未知' },
  { selector: '.script-id', prop: 'text', attr: 'href', fallback: '未知' },
  { selector: '.version', prop: 'text', fallback: '未知' },
  { selector: '.score .count', prop: 'text', fallback: '0' },
  { selector: '.daily-installs .count', prop: 'text', fallback: '0' },
  { selector: '.total-installs .count', prop: 'text', fallback: '0' },
  { selector: '.good-rating .count', prop: 'text', fallback: '0' },
  { selector: '.bad-rating .count', prop: 'text', fallback: '0' },
  { selector: '.update-time .content', prop: 'text', fallback: '未知' },
]

// 徽章配置
export const badgeConfig: IBadgeConfig[] = []

export enum ControlsType {
  onlySearch = 'onlySearch',
  hideDisliked = 'hideDisliked',
  hideFailed = 'hideFailed',
  syncSearch = 'syncSearch',
}
